package com.upcode.weighing.util;

import com.upcode.weighing.vo.common.DateParam;
import com.upcode.weighing.vo.common.ParamParse;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * v1.0.6时间处理工具类
 * @author maoyh
 */
public class DateParamUtils {
    /**
     * 获取n月最后一天
     * @param dt
     * @return
     */
    private static String getLastMonthLastDay(String dt, int i) {
        DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate beginDateTime = LocalDate.parse(dt, inFormat);
        LocalDate dateMonth = beginDateTime.minusMonths(i);
        LocalDate lastMonth = dateMonth.with(TemporalAdjusters.lastDayOfMonth());
        return lastMonth.format(inFormat);
    }


    /**
     * 生成时间参数
     * @param starTime 开始时间
     * @param endTime 结束时间
     * @param monthTag 为true直接生成每日
     * @return
     */
    public static List<ParamParse> getBetweenDate1(String starTime, String endTime, Boolean monthTag){
        starTime = starTime.substring(0,10);
        endTime = endTime.substring(0,10);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<ParamParse> list = new ArrayList<>();
        LocalDate startDateTime = LocalDate.parse(starTime, dateTimeFormatter);
        LocalDate endDateTime = LocalDate.parse(endTime, dateTimeFormatter);
        Period pe = Period.between(startDateTime,endDateTime);
        if(pe.getMonths()<1 || monthTag){
            long day = endDateTime.toEpochDay() - startDateTime.toEpochDay();
            for(int i = 0; i<day; i++){
                String dt = endDateTime.plusDays(-i).format(dateTimeFormatter);
                dateParse(list, dt, 4, 0);
            }
            // 大于1个月，大于等于6个月，展现每月15，月底
        }else if(pe.getMonths()>=1 && pe.getMonths()<6){
            //最大时间单独处理
            if(Integer.parseInt(endTime.split("-")[2])>15){
                dateParse(list,endTime,1,0);
            }else {
                dateParse(list,endTime,2,0);
            }
            //补全中间时间段
            for(int i=1; i<=pe.getMonths(); i++){
                //月底参数
                dateParse(list,endTime,1,i);
                //15构造
                dateParse(list,endTime,2,i);
            }
            //最小时间单独处理
            if(Integer.parseInt(starTime.split("-")[2])>15){
                dateParse(list,starTime,1,0);
            }else {
                dateParse(list,starTime,2,0);
            }
        }else {
            if(Integer.parseInt(endTime.split("-")[2])>15){
                dateParse(list,endTime,3,-1);
            }
            for(int j=0; j<=pe.getMonths(); j++){
                dateParse(list,endTime,3,j);
            }
        }
        list = list.stream().sorted(Comparator.comparing(ParamParse::getDataDate)).collect(Collectors.toList());
        return list;
    }

    /**
     *
     * @param list
     * @param dateTime
     * @param tag 【1：月底】，【 2：每月15号】,[3：大于6月的]
     */
    private static void dateParse(List<ParamParse> list, String dateTime, int tag, int month){
        ParamParse paramParse = new ParamParse();
        String endLast = getLastMonthLastDay(dateTime,month);
        if(tag == 1){
            String start15 = endLast.substring(0,8)+"15";
            //月底参数
            DateParam dateParam1 = new DateParam();
            dateParam1.setStartTime(start15);
            dateParam1.setEndTime(endLast);
            paramParse.setDataDate(endLast);
            paramParse.setParam(dateParam1);
            list.add(paramParse);
        }else if(tag == 2){
            String start15 = endLast.substring(0,8)+"15";
            String start01 = endLast.substring(0,8)+"01";
            //15构造
            DateParam dateParam2 = new DateParam();
            dateParam2.setStartTime(start15);
            dateParam2.setEndTime(start01);
            paramParse.setDataDate(start15);
            paramParse.setParam(dateParam2);
            list.add(paramParse);
        }else if(tag == 3){
            //本月15
            String curr15 = endLast.substring(0,8)+"15";
            String lstEnd = getLastMonthLastDay(dateTime,month+1);
            //上月15
            String last15 = lstEnd.substring(0,8)+"15";
            //15构造
            DateParam dateParam2 = new DateParam();
            dateParam2.setStartTime(last15);
            dateParam2.setEndTime(curr15);
            paramParse.setDataDate(curr15);
            paramParse.setParam(dateParam2);
            list.add(paramParse);
        }else if(tag == 4){
            DateParam dateParam2 = new DateParam();
            dateParam2.setStartTime(dateTime);
            dateParam2.setEndTime(dateTime);
            paramParse.setDataDate(dateTime);
            paramParse.setParam(dateParam2);
            list.add(paramParse);
        }
    }
}
